package org.voice;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class MyTTS {

    public static void main(String[] args) {
        MyTTS tts = new MyTTS();
        tts.say("开始录音，请说话...");
        tts.release();
    }

    private Dispatch dis;
    private ActiveXComponent activeXComponent;

    {
        //将 文件jacob-1.20-x64.dll、jacob-1.20-x86.dll 拷贝到 C:\Windows\System32 目录下。
        //调用windowsApi 的 com组件，Sapi.spVoice是 windows com组件名称
        activeXComponent = new ActiveXComponent("Sapi.SpVoice");
        //从com组件中获得调度目标
        dis = activeXComponent.getObject();
        try {
            // 设置语速，范围从 -10 到 10，值越大语速越快
            Dispatch.put(dis, "Rate", new Variant(2));
            // 设置音量，范围从 0 到 100
            Dispatch.put(dis, "Volume", new Variant(100));
        } catch (Exception e) {
            e.printStackTrace();
            release();
        }
    }

    public void say(String txt) {
        Dispatch.call(dis, "Speak", new Variant(txt));
    }

    public void release() {
        System.out.println("关闭语音播放器");
        dis.safeRelease();
        activeXComponent.safeRelease();
    }

}

